﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///AdminNava 的摘要说明
/// </summary>
public partial class AdminNava
{
   
    public static List<AdminNava> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            List<AdminNava> AdminNavas = new List<AdminNava>();
            if (HttpContext.Current.Application["AdminNava"] != null)
            {
                AdminNavas = (List<AdminNava>)HttpContext.Current.Application["AdminNava"];
            }
            else
            {
                AdminNavas = context.AdminNava.ToList();
                HttpContext.Current.Application["AdminNava"] = AdminNavas;
            }
            return AdminNavas;
        }
    }
    public static List<AdminNava> GetAllByAdminRoleID(int AdminRoleID)
    {

        return GetAll().Where(row => row.RoleID == AdminRoleID).ToList();

    }
    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            HttpContext.Current.Application["AdminNava"] = null;
            context.AdminNava.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            HttpContext.Current.Application["AdminNava"] = null;
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<AdminNava>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            HttpContext.Current.Application["AdminNava"] = null;
            var objItem = context.AdminNava.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.AdminNava.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
    public static void DeleteAllByRoleID(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            HttpContext.Current.Application["AdminNava"] = null;
            context.ExecuteCommand(@"delete from  dbo.AdminNava  where RoleID  = {0} ", id);
        }
    }
}